<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TaskController
 *
 * @author QuangKhoi
 */
class TaskController {

    /**
     * Store model object
     * @var Task
     */
    private $model;

    /**
     * Store view for the stream
     * @var TaskView
     */
    private $view;

    /**
     * 
     * @param type $model
     * @param type $view
     */
    public function __construct($model, $view) {
        $this->model = $model;
        $this->view = $view;
    }

    public function render() {
        //Need to edit
        $db = Database::getInstance();
        $tasks = array();
        $userId = $_SESSION["userid"];
        $result = $db->Query("SELECT * FROM tasks WHERE taskuserid = '$userId' OR taskassignedto = '$userId' ORDER BY taskdatecreated DESC");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $rows = $result->dataTable->rows;
                foreach ($rows as $row) {
                    $tasks[] = Task::withId($row["taskid"]);
                }
            }
        }

        $this->view->model = $this->model;
        $this->view->tasks = $tasks;
        $masterView = MasterView::getInstance();
        $masterView->taskView = $this->view;
    }

    public function save() {
        $model = $this->model;
        $query = "SELECT * FROM Tasks WHERE taskid = '$model->taskId'";
        $result = Database::getInstance()->query($query);
        if ($result->returnCode == 200)  //OK
            if ($result->dataTable->numOfRow == 1) {
                $dateCreated = $model->taskDateCreated->format("Y-m-d H:i:s");
                Database::getInstance()->query("UPDATE Tasks
                        SET taskname='$model->taskName', 
                        taskcontent='$model->taskContent', 
                        taskuserid='$model->taskUserId', 
                        taskassignedto='$model->taskAssignedTo', 
                        taskdatecreated='$dateCreated', 
                        tasktype='$model->taskType', 
                        signalid='$model->signalId', 
                        status ='$model->status'
                        WHERE taskid = '$model->taskId'");
                return $model;
            } else {
                $dateCreated = $model->taskDateCreated->format("Y-m-d H:i:s");
                $model->taskId = Database::getID("Tasks", "taskid");
                Database::getInstance()->query("INSERT INTO Tasks 
                            (taskname, taskcontent, taskuserid, taskassignedto, 
                            tasktype, signalid, status, taskdatecreated) VALUES
                            ('$model->taskName', '$model->taskContent',
                            '$model->taskUserId', '$model->taskAssignedTo',
                            '$model->taskType', '$model->signalId', 
                            '$model->status', '$dateCreated')");
                return $model;
            }
        return NULL;
    }

    public function markComplete() {
        $this->model->status = 2;
        return $this->save();
    }
    
    public function undoComplete() {
        $this->model->status = 1;
        return $this->save();
    }

    public function delete() {
        $taskId = $this->model->taskId;
        $result = Database::getInstance()->query("DELETE FROM tasks WHERE taskid='$taskId'");
        if ($result->returnCode == 200)
            return true;
        return false;
    }
}

?>
